<?php
namespace app\faxing\controller;

use app\common\controller\FaXingBase;
use app\common\model\Order;
use app\common\model\OrderLog;
use app\common\model\PlayerRole;
use think\Db;
use think\facade\Log;

/**
 * 后台首页
 * Class Index
 * @package app\admin\controller
 */
class Pay extends FaXingBase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function pay(){
        $data = $this->request->param();
        $order = $this->channelOrder($data);

        $channelName = ucfirst($this->channel['username']);
        // 动态加载自定义类（例如：app\common\CustomClass
        $className ='app\faxing\channel\\'.$channelName; // 替换为你的类名
        if (!class_exists($className)) {
            Log::info("渠道类初始化失败");
        }
        //订单校验
        $gameapi = new $className();
        $return = $gameapi->order($order);
        if(empty($return)){
            $return['order_no'] = $order['order_no'];
        }
        $this->success("成功","",$return);
    }


    protected function channelOrder($data)
    {
        if(empty($data['sign'])){
            $this->error('sign不能为空');
        }
        if(empty($data['type'])){
            $this->error('请选择支付方式');
        }
        if(empty($data['amount']) || $data['amount'] <= 0){
            $this->error('支付金额错误');
        }
        if(empty($data['goods_name'])){
            $this->error('商品名称不能为空');
        }
        //校验提交的游戏id、uid、cid
        if(empty($data['gid']) || $data['gid'] != $this->gid){
            $this->error('游戏id错误');
        }
        $grr = [30];
        $uid = $data['uid'];
        if(in_array($data['gid'], $grr)){
            $player_old = DB::connect('db_config_aiqi')->name('player_old')->where(['banma_id'=>$data['uid']])->find();
            if($player_old){
                $uid = $player_old['banma_id'];
            }
        }

        $old_grr = [81,82,83,84,85,86,87,88,86,91,
            92,93,94,95,96,97,98,99,100,101,102];

        if(in_array($data['gid'], $old_grr)){
            $player_old = DB::connect('db_config_aiqi')->name('jinling_palyer')->where(['old_id'=>$data['uid']])->find();
            if($player_old){
                $uid    = $player_old['old_id'];
            }
        }

        if(empty($data['uid']) || $data['uid'] != $uid){
            $this->error('用户id错误');
        }
        if(!empty($data['cid']) && $data['cid'] != $this->cid){
            $this->error('游戏渠道id错误');
        }

        $sid = $data['sid'] ?? 0;

        unset($data['type']);
        unset($data['cid']);
        unset($data['sid']);
        unset($data['token']);

        if(!$this->checkSign($data,2)){
            $this->error('签名错误');
        }

        $data['type'] = 3;
        $data['num'] = $data['num'] ?? 1;
        $data['sid'] = $sid;
        $data['uid'] = $uid;
        $data['gid'] = $this->gid;
        $data['cid'] = $this->cid;
        $data['is_test'] = 0;
        if($this->game['gstatus'] == 2 || in_array($this->gid,[10,11])){
            $data['is_test'] = 1;
        }
        try {
            //创建订单
            $order = $this->create_order($data);
            return $order;
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
    }

    protected function create_order($data)
    {
        $data['order_no'] = get_order_sn($data['uid'],$this->gid,$data['type']);
        $order = new Order();
        $orderId = $order->creatOrder($data);
        $order =  Order::find($orderId);
        OrderLog::addLog($order->order_no, $order->order_status, $order->type);
        return $order;
    }


}